<template>
	<view>
		<view class="header">
			<x-status-bar></x-status-bar>
			<view class="flex-center-between header-navbar">
				<x-back-btn></x-back-btn>
				<view class="u-f-40 font-bold common-title">
					{{ lang_message.message }}{{ messageCount ? `(${ messageCount })` : '' }}
				</view>
				<view class="flex-center u-f-28" @click="clearUnRead()">
					<image src="@/static/image/message/edit_icon.png" class="clear-flex u-m-r-8"
						style="width: 32rpx; height: 32rpx;"></image>

					<view class="clear-flex u-lh-40">{{ lang_message.clearUnRead }}</view>
				</view>
			</view>
		</view>
		<view>
			<mescroll-body height="calc(100vh - 98rpx - var(--status-bar-height))" ref="mescrollRef"
				@init="mescrollInit" @down="downCallback" @up="upCallback">
				<view style="padding: 0 30rpx;width: 100%;">
					<view v-for="(item, index) in messageTypes" :key="index" class="flex-center"
						style="padding: 16rpx 0;" @click="jumpUrl(item.page)">
						<image :src="item.icon" class="clear-flex u-m-r-18" style="width: 110rpx; height: 110rpx;">
						</image>
						<view style="padding: 12rpx 0;width: 100%;" class="flex-between flex-column">
							<view class="flex-center-between">
								<view class="u-f-30 u-lh-36 font-medio">{{ lang_message[item.labelKey] }}</view>
								<view class="u-f-22 u-lh-32 nowrap" style="color: #B8B8B9;">
									{{ dealTime(dealData(item, 'createTime')) }}
								</view>
							</view>

							<view class="u-m-t-10 flex-between linecamp-1">
								<view class="u-f-28 u-lh-40 color-grew linecamp-1" style="width: 520rpx;">
									{{ dealData(item, 'title') }}
								</view>
								<view v-if="messageInfo[`unRead${ item.value }NotifyCount`]"
									class="u-f-24 u-lh-36 u-avatar-34 color-white flex-center-center"
									style="background-color: #FF3A2F;">
									{{ messageInfo[`unRead${ item.value }NotifyCount`] }}</view>
							</view>
						</view>
					</view>

					<!-- 留空 -->
					<view style="height: 200rpx"></view>
				</view>
			</mescroll-body>
		</view>
	</view>
</template>

<script>
	import {
		notifyUserNotifyInfo,
		notifyReadAll
	} from '@/api/base.js'
	import {
		messageNotifyPage
	} from '@/api/message.js'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			let messageTypes = [{
					label: '系统消息',
					labelKey: 'systemMessage',
					key: 'system',
					value: 'System'
				},
				{
					label: '交易物流通知',
					labelKey: 'logisticsNotice',
					key: 'logistics',
					value: 'Order'
				}
			].map((item, index) => ({
				...item,
				icon: require(`@/static/image/message/message_icon${ index + 1 }.png`),
				page: `/pages/common/message/${ item.key }`
			}))
			return {
				messageTypes,

				refresh: false
			}
		},
		computed: {
			messageCount() {
				let { unReadSystemNotifyCount, unReadOrderNotifyCount } = this.messageInfo
				return unReadSystemNotifyCount + unReadOrderNotifyCount
			},
		},
		onShow() {
			this.downCallback()
		},
		methods: {
			dealData(item, key) {
				let obj = this.messageInfo[`last${ item.value }Notify`] || {}
				return obj[key] || ''
			},
			// 处理时间
			dealTime(time = '') {
				if (time) {
					time = this.dayjs(time).format('YYYY/MM/DD')
				}
				return time
			},
			// 清除未读
			clearUnRead() {
				if (this.messageTypes.findIndex(item => item[`unRead${ item.value }NotifyCount`]) > -1) {
					notifyReadAll({}).then((data) => {
						this.$api.msg('消息全部已读')
						this.upCallback()
					})
				}
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			async upCallback(page) {
				let node = this.$refs['mescrollRef']
				let status = await this.$store.dispatch('updateMessage')
				this.refresh = false
				if (status) {
					node && node.mescroll.endSuccess(10);
				} else {
					node && node.mescroll.endErr();
				}
			},
			downCallback() {
				if (this.refresh) return
				this.refresh = true
				let node = this.$refs['mescrollRef']
				node && node.mescroll.resetUpScroll()
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>
